layui.define(['index', 'layer', 'form', 'table', 'laypage', 'layedit', 'laydate' ], function (exports) {
    var $ = layui.$;
    var table = layui.table;
    var laypage = layui.laypage;
    var form = layui.form;
    var element = layui.element;
    var laydate = layui.laydate;
    let formSelects = layui.formSelects;
    var layer = layui.layer;
    var existShopIds = new Array();

    var tagElems = $('#addShopDiv');
    var initShopSpan = tagElems[0].children;
    for (let i = 0; i < initShopSpan.length; i++) {
        existShopIds.push(initShopSpan[i].attributes['value'].nodeValue);
    }

    let closeBtn = '<i style="font-style: normal;margin-left: 10px;cursor: pointer;">X</i>' ;
    form.on('select(act-shop)', function(data){

        if (data.value === ''){
            return;
        }
        if ( existShopIds.includes(data.value)){
            layer.msg('该店铺已选', {icon: 5, shift: 6});
            return;
        }
        var selectIndex = data.elem.selectedIndex;
        var option = data.elem.options[selectIndex];

        existShopIds.push(option.value);
        $('#addShopDiv').append('<span class="cityprice" value = ' + option.value + ' style="display: inline-block;width: auto;border: 1px solid #ccc;border-radius: 10px;padding: 5px 20px; margin-right: 5px;tmargin-bottom: 5px;background-color: #009688;color:#fff;">' + option.text + closeBtn + '</span>');
    });

    $('#addShopDiv').on('click', 'span i', function () {
        var spanData = $(this).parent().remove();
        var shopId = spanData[0].attributes['value'].nodeValue;
        for (let i = 0; i < existShopIds.length; i++) {
            if (existShopIds[i] === shopId) {
                existShopIds.splice(i, 1);zmnMothedUtil.closeThisIframeMsg
                i--;
            }
        }
        return false;
    });

    // zmnLayUtil.layFormVerify(form);
    laypage.render(zmnPage.laypageOptions(json));

    laydate.render({
        elem: '#datetime-range', //指定元素
        format: 'yyyy-MM-dd HH:mm:ss',
        type: 'datetime',
        range: true
    });

    form.verify({
        name: function(value, item){ //value：表单的值、item：表单的DOM对象
            value = value || '';
            if(value.length > 40){
                return '活动名称不能太长';
            }
        }
        , showName: function(value, item){
            value = value || '';
            if(value.length > 40){
                return '显示名称不能太长';
            }
        }
        , promotionContent: function(value, item){
            if(value.length > 100){
                return '促销语不能太长';
            }
        }
        , introduction: function(value, item){
            if(value.length > 100){
                return '简介不能太长';
            }
        }
    });
    form.render();
    var hasDatas = json.hasDatas;
    if(hasDatas){
        var currentActivityId = json.currentActivityId;
        var infoVo = json.detail.infoVo;
        var actPlatList = json.actPlatList;
        // var platList = json.platList;
        // var typeList = json.typeList;
        // var statusList = json.statusList;
        var platIdArr = new Array();
        $.each(actPlatList, function(index, item){
            platIdArr.push(item.activityPlatId);
        });

        console.info("platIdArr:"+JSON.stringify(platIdArr));
        var startAndEndDate = zmnUtil.createTime(infoVo.startTime) + " - " + zmnUtil.createTime(infoVo.endTime);
        var oldData = {
            "activityId": currentActivityId
            ,"name": infoVo.name
            ,"showName": infoVo.showName
            ,"startAtAndEndAt": startAndEndDate
            ,"promotionContent": infoVo.promotionContent
            ,"introduction": infoVo.introduction
            // ,"platIdArr" : platIdArr
            ,"activityType": infoVo.activityType
        };
        let plat_checkbox_startWith = "platId[";
        let plat_checkbox_endWith = "]";
        $.each(platIdArr, function(i, platId){
            var attr = plat_checkbox_startWith + platId + plat_checkbox_endWith;
            oldData[attr] = true;
        });

        form.val("act-base-form", oldData);

    }

    form.on('submit(act-modify-base-submit)', function (obj) {
        var data = obj.field;
        let plat_checkbox_startWith = "platId[";
        var platIds = new Array();
        $.each(data, function(attr, value){
            var flag = attr.startsWith(plat_checkbox_startWith);
            if(flag){
                platIds.push(value);
            }
        });
        data['platIds'] = platIds;
        data['shopIds'] = existShopIds;
        /*if(platIds.length == 0){
            layer.msg('活动平台必选', {icon: 5, shift: 6});
            return false;
        }*/

        $("#act-modify-base-submit").addClass('layui-disabled').prop("disabled", true);
        $.ajax({
            type: "POST",
            url: "/activity/config/modify-base",
            contentType: "application/json",
            dataType: "json",
            data: JSON.stringify(data),
            success: function (res) {
                //zmnMothedUtil.closeThisIframeMsg(parent, json.message, "zmn-coup-table");
                console.info('新增活动 activityId:' + res.data);
                if (res.status == 200) {
                    if(!hasDatas){
                        json.currentActivityId = res.data;
                        // 新增就关闭当前窗口
                        zmnMothedUtil.closeThisIframeMsg(parent, res.message, "zmn-activity-table");
                    }
                    // 不关闭
                    // zmnMothedUtil.closeThisIframeMsg(parent, res.message, "zmn-activity-table");
                    layer.msg('保存成功!');
                }else{
                    layer.msg(res.message, {icon: 5, shift: 6});
                }
                $("#act-modify-base-submit").removeClass('layui-disabled').prop("disabled", false);
                return false;
            }
        });

        return false;
    });

    $(document).on('click', '#act-modify-base-cancel', function () {
        zmnMothedUtil.zmnMothedUtil.closeThisIframeMsg(parent, null, null);
    });

    exports('zmn_plat_activity_baseInfo', {});
});


// function zmnMothedUtil.closeThisIframeMsg(parent, msg, parentTableId) {
//     if (parentTableId) {
//         parent.layui.table.reload(parentTableId);
//     }
//     //当你在iframe页面关闭自身时
//     let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
//     parent.layer.close(index); //再执行关闭
//     if (msg) {
//         parent.layer.msg(msg, {"icon": 1});
//     }
//
// }

